# @Time : 2021/10/25 20:29
# @Author : Li Kunlun
# @Description : 线程池测试
import concurrent.futures
import chapter08.py02_spider02.blog_spider as blog_spider

# craw
with concurrent.futures.ThreadPoolExecutor() as pool:
    htmls = pool.map(blog_spider.craw, blog_spider.urls)
    htmls = list(zip(blog_spider.urls, htmls))  # 构成元组
    for url, html in htmls:
        # 按照顺序执行
        # https://www.cnblogs.com/#p1 69918
        # https://www.cnblogs.com/#p2 69918
        # https://www.cnblogs.com/#p3 69918
        # ...
        print(url, len(html))
print("craw over")

# parse
with concurrent.futures.ThreadPoolExecutor() as pool:
    futures = {}  # 字典
    for url, html in htmls:
        future = pool.submit(blog_spider.parse, html)
        futures[future] = url
    # 方式1
    # for future, url in futures.items():
    #     # 按照顺序执行
    #     print(url, future.result())

    # 方式2
    for future in concurrent.futures.as_completed(futures):
        url = futures[future]
        # 不按顺序执行
        print(url, future.result())
